/*
 * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifdef RECORDER_SUPPORT
#define LOG_TAG "main"

#include <iostream>
#include <memory>
#include <thread>
#include <chrono>

#include "media_errors.h"
#include "i_engine_factory.h"

extern "C" {
__attribute__((visibility("default"))) OHOS::Media::IEngineFactory* CreateEngineFactory();
}

using namespace OHOS::Media;

class RecorderEngineObs : public IRecorderEngineObs {
public:
    ~RecorderEngineObs() override = default;

    void OnError(ErrorType errorType, int32_t errorCode) override
    {
        std::cout<< "error"<< errorCode <<std::endl;
    }

    void OnInfo(InfoType type, int32_t extra) override
    {

    }
};
#define PRINT_IF_FAIL(exec) \
do { \
    auto tmp = (exec); \
    if (tmp != OHOS::ERR_OK) { \
        printf("" #exec "failed with %d", tmp); \
        return -1; \
    } \
} while (0)

int main(int argc, char* argv[])
{
    std::cout << "Use media_standard interface recorder." << std::endl;
    auto engineFactory = std::unique_ptr<OHOS::Media::IEngineFactory>(CreateEngineFactory());
    auto recorder = engineFactory->CreateRecorderEngine();
    auto obs = std::make_shared<RecorderEngineObs>();
    PRINT_IF_FAIL(recorder->SetObs(obs));
    int32_t sourceId= 0;
    PRINT_IF_FAIL(recorder->SetAudioSource(AUDIO_MIC,sourceId));
#ifdef VIDEO_SUPPORT
    int32_t videoSourceId= 0;
    recorder->SetVideoSource(VideoSourceType::VIDEO_SOURCE_SURFACE_YUV,videoSourceId);
    recorder->SetOutputFormat(OutputFormatType::FORMAT_MPEG_4);
#else
    PRINT_IF_FAIL(recorder->SetOutputFormat(OutputFormatType::FORMAT_M4A));
#endif
    auto audSampleRate = AudSampleRate{44100};
    auto audChannel = AudChannel{2};
    auto audBitRate = AudBitRate{320000};
    auto auEncoder = AudEnc{AudioCodecFormat::AAC_LC};
    auto maxDuration = MaxDuration{60};
    auto maxFileSize = MaxFileSize {100 * 1024 * 1024};
    PRINT_IF_FAIL(recorder->Configure(sourceId, audSampleRate));
    PRINT_IF_FAIL(recorder->Configure(sourceId, audChannel));
    PRINT_IF_FAIL(recorder->Configure(sourceId, audBitRate));
    PRINT_IF_FAIL(recorder->Configure(sourceId, auEncoder));
#ifdef VIDEO_SUPPORT
    auto viRectangle = VidRectangle{1920,1080};
    auto viBitRate = VidBitRate{320000};
    auto viFrameRate = VidFrameRate{320000};
    auto viEncoder = VidEnc{VideoCodecFormat::H264};

    recorder->Configure(videoSourceId, viRectangle);
    recorder->Configure(videoSourceId, viBitRate);
    recorder->Configure(videoSourceId, viFrameRate);
    recorder->Configure(videoSourceId, viEncoder);
#endif
    auto outFilePath = OutFilePath{"/tmp"};
    PRINT_IF_FAIL(recorder->Configure(DUMMY_SOURCE_ID, outFilePath));
    PRINT_IF_FAIL(recorder->Configure(DUMMY_SOURCE_ID, maxDuration));
    PRINT_IF_FAIL(recorder->Configure(DUMMY_SOURCE_ID, maxFileSize));
    PRINT_IF_FAIL(recorder->Prepare());
    PRINT_IF_FAIL(recorder->Start());
    std::cin.get();
    PRINT_IF_FAIL(recorder->Pause());
    PRINT_IF_FAIL(recorder->Resume());
    return 0;
}
#endif //RECORDER_SUPPORT